//
//  ZBHomeIGListViewController.m
//  XZBProduct
//
//  Created by xzb on 2018/12/25.
//  Copyright © 2018 xzb. All rights reserved.
//

#import "ZBHomeIGListViewController.h"
#import <IGListKit.h>
#import "ZBProductSectionController.h"
#import "ZBMartHomeModel.h"
#import "ZBTextCell.h"

@interface ZBHomeIGListViewController () <IGListAdapterDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) IGListAdapter *adapter;
@property (nonatomic, strong) ZBMartHomeModel *homeModel;

@end

@implementation ZBHomeIGListViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (@available(iOS 10.0, *)) {
        if ([[UICollectionView class] instancesRespondToSelector:@selector(setPrefetchingEnabled:)]) {
            [[UICollectionView appearance] setPrefetchingEnabled:NO];
        }
    }
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"homeGet" withExtension:@"json"];
            NSData *data = [NSData dataWithContentsOfURL:fileURL];
            ZBMartHomeModel *homeModel = [ZBMartHomeModel yy_modelWithJSON:data];
            self.homeModel = homeModel;
            if (self.homeModel) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.collectionView.mj_header endRefreshing];
                    [self.adapter performUpdatesAnimated:YES completion:nil];
                });
            }
        });
    }];
    
    
    self.title = @"IGList";
    self.view.backgroundColor = [UIColor cyanColor];
    
    self.adapter = [[IGListAdapter alloc] initWithUpdater:[IGListAdapterUpdater new] viewController:self];
    self.adapter.dataSource = self;
    self.adapter.collectionView = self.collectionView;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setBackgroundColor:[UIColor blackColor]];
    rightBtn.bounds = CGRectMake(0, 0, 40, 40);
    @weakify(self);
    [rightBtn bk_addEventHandler:^(id _Nonnull sender) {
        @strongify(self);
        self.homeModel.items = @[];
         [self.adapter performUpdatesAnimated:NO completion:nil];
     
    } forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationBar.rightItems = @[rightBtn];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.collectionView.frame = CGRectMake(0, self.navigationBarBottom, self.view.width, self.view.height - self.navigationBarBottom);
}

- (UIView *)emptyViewForListAdapter:(IGListAdapter *)listAdapter
{
    return nil;
}

- (NSArray<id<IGListDiffable> > *)objectsForListAdapter:(IGListAdapter *)listAdapter
{
    return self.homeModel.items;
}

- (IGListSectionController *)listAdapter:(IGListAdapter *)listAdapter sectionControllerForObject:(id)object
{
    ZBProductSectionController *productSection = [[ZBProductSectionController alloc] init];
    return productSection;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flow = [UICollectionViewFlowLayout new];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
        _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_collectionView registerClass:[ZBTextCell class] forCellWithReuseIdentifier:@"ZBTextCell"];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

@end
